package com.ishopping.service.manage.service.impl;

import com.ishopping.common.core.base.service.AbstractBaseService;
import com.ishopping.service.manage.mapper.UserRoleMapper;
import com.ishopping.api.manage.entity.UserRole;
import com.ishopping.api.manage.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userRoleService")
public class UserRoleServiceImpl extends AbstractBaseService<UserRole> implements IUserRoleService{
	@Autowired
	private UserRoleMapper userRoleMapper;

	/**
	 * 通过userId获取用户角色
	 * @param userId
	 * @return List<Long>
	 */
	@Override
	public List<UserRole> findUserRolesByUserId(Long userId) {
		return userRoleMapper.queryUserRolesByUserId(userId);
	}

	/**
	 * 更新用户关联的角色
	 * @param userId
	 * @param newRoleList
	 * @return int
	 */
	@Override
	@Transactional
	public int save(Long userId, List<Long> newRoleList) {
		// 批量删除
		deleteByUserId(userId);

		// 批量插入
		int result = batchInsert(userId, newRoleList);
		return result;
	}

	/**
	 * 通过userId删除其关联的角色
	 * @param userId
	 * @return int
	 */
	@Override
	public int deleteByUserId(Long userId) {
		return userRoleMapper.deleteByUserId(userId);
	}

	/**
	 * 批量插入用户关联的角色
	 * @param newRoleList
	 * @param userId
	 * @return int
	 */
	@Override
	public int batchInsert(Long userId, List<Long> newRoleList) {
		return userRoleMapper.batchInsert(userId, newRoleList);
	}
}